package multiple;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import multiple.dao.OrderMapper;
import multiple.dao.UserMapper;
import pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author Feuler
 * @since 2020/11/10
 **/

public class MultipleTest {
    final InputStream inputStream = Resources.getResourceAsStream("multiple/sqlMapConfig.xml");
    final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    final SqlSession sqlSession = sqlSessionFactory.openSession(true);
    final OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
    final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    public MultipleTest() throws IOException {
    }

    @After
    public void after() {
        sqlSession.close();
    }

    @Test
    public void test1() {
        try {
            orderMapper.findOrderAndUser().forEach(System.out::println);
            System.out.println("test 1 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test2() {
        try {
            userMapper.findAll().forEach(System.out::println);
            System.out.println("test 2 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test3() {
        try {
            userMapper.findAllUserAndRole().forEach(System.out::println);
            System.out.println("test 3 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test4() {
        try {
            System.out.println("before add");
            userMapper.selectUser().forEach(System.out::println);

            User user = new User();
            user.setId(Long.valueOf(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()).intValue());
            user.setUsername("user" + LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli());
            userMapper.addUser(user);
            System.out.println("after add");
            userMapper.selectUser().forEach(System.out::println);

            user.setUsername("user" + LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli());
            userMapper.updateUser(user);
            System.out.println("after update");
            userMapper.selectUser().forEach(System.out::println);

            userMapper.deleteUser(user);
            System.out.println("after delete");
            userMapper.selectUser().forEach(System.out::println);

            System.out.println("test 4 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test5() {
        try {
            orderMapper.findOrderAndUserWithAnnotation().forEach(System.out::println);
            System.out.println("test 5 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test6() {
        try {
            userMapper.findUserAndOrderWithAnnotation().forEach(System.out::println);
            System.out.println("test 6 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test7() {
        try {
            userMapper.findUserAndRoleWithAnnotation().forEach(System.out::println);
            System.out.println("test 7 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }
}
